<template>
    <el-dialog :title="dialogTitle" :visible.sync="isVisible">
        <el-form ref="productCateForm" :model="productCateForm" :rules="rules" label-width="100px">
            <el-form-item label="上级" prop="parentId">
                <el-select ref="selectReport" v-model="productCateForm.parentId"
                           placeholder="一级分类" style="width: 180px;"
                >
                    <el-option :value="productCateForm.parentId" :label="productCateForm.parentName"
                               style="width: 180px; height: 200px; overflow: auto; background-color: #fff;"
                    >
                        <el-tree
                            default-expand-all
                            :props="defaultProps"
                            :data="treeSelectList"
                            @node-click="handleNodeClick"
                        />
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="名称" prop="label">
                <el-input v-model="productCateForm.label" />
            </el-form-item>
            <el-form-item label="分类图" prop="imgUrl">
                <image-upload :url.sync="productCateForm.imgUrl" action="action" :data="uploadData" name="file" :width="100" :height="100" @uploadSuccess="uploadSuccess"/>
            </el-form-item>

            <el-form-item label="排序" prop="sortValue">
                <el-input v-model="productCateForm.sortValue" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" plain size="small" @click="submitForm('productCateForm')">保存</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import productCateApi from '@/api/init/productCate'
export default {
    name: 'Edit',
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        },
        type: {
            type: String,
            default: 'add'
        },
        title: {
            type: String,
            default: '添加'
        }
    },
    data() {
        return {
            // 因为不能直接修改 props 接收到的值，所以用一个变量来接收，修改这个变量
            dialogType: this.type,
            dialogTitle: this.title,
            productCateForm: {
                parentId: 0,
                parentName: '一级分类',
                label: '',
                imgUrl: '',
                sortValue: 0
            },
            rules: {
                label: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                ]
            },
            treeSelectList: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            // 图片上传组件参数
            uploadData: {'bizId': ''}
        }
    },
    computed: {
        isVisible: {
            get() {
                return this.dialogVisible
            },
            set() {
                this.close()
            }
        }
    },
    methods: {
        getTableData(tableData) {
            this.treeSelectList = tableData.concat()
            this.treeSelectList.unshift({id: 0, label: '一级分类'})
        },
        close() {
            this.$emit('close')
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate(valid => {
                if (valid) {
                    // 判断是添加还是修改
                    if (_this.dialogType == 'add') {
                        productCateApi.add(this.productCateForm).then(() => {
                            _this.isVisible = false
                            _this.$emit('success')
                        })
                    } else {
                        productCateApi.update(this.productCateForm).then(() => {
                            _this.isVisible = false
                            _this.$emit('success')
                        })
                    }
                } else {
                    return false
                }
            })
        },
        getProductCate(id) {
            // 清空表单
            this.resetForm('productCateForm')
            // 回显数据
            productCateApi.get(id).then(res => {
                if (res.data.data.parentId == 0) {
                    res.data.data.parentName = '一级分类'
                }
                this.productCateForm = res.data.data
            })
        },
        resetForm(formName) {
            this.productCateForm.parentId = 0
            this.productCateForm.parentName = '一级分类'
            this.$refs[formName].resetFields()
        },
        handleNodeClick(node) {
            this.productCateForm.parentName = node.label
            this.productCateForm.parentId = node.id
        },
        // 图片上传成功后赋值到表单
        uploadSuccess(res) {
            this.productCateForm.imgUrl = res.data.data.url
        }
    }
}
</script>
